package com.han.hotelplat.handle;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.base.BaseRamPo;
import com.han.hotelplat.comm.utils.DateUtil;
import com.han.hotelplat.comm.utils.LogUtil;
import com.han.hotelplat.dao.hotel.HotelDao;
import com.han.hotelplat.dao.wallet.WalletDao;
import com.han.hotelplat.pojo.hotel.Hotel;
import com.han.hotelplat.pojo.task.Task;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.service.task.impl.TaskServiceImpl;

@Component
public class TaskHandle{
	public Task task;
	@Autowired
	private WalletDao walletDao;
	@Autowired
	private HotelDao hotelDao;
	
	public TaskHandle() {
	}
	
	public TaskHandle(Task po) {
		this.task = po;
	}
	
	// 查询报名人数
	public Integer getEmcount() {
		String hql="select count(*) from Emtask where taskid="+task.getId();
		return walletDao.findCount(hql, null);
	}
	
	// 查询已清洁天数
	public Integer getDays() {
		return DateUtil.daysDistance(task.getStarttime(),new Date());
	}
	
	//已清洁房间数
	public Integer getRooms() {
		String sql="select sum(roomcount) from tb_child_task where result=1 taskid="+task.getId();
		try{
			return Integer.parseInt(walletDao.findBySql(sql).get(0).toString());
		}catch(Exception e){
			return 0;
		}
	}
	
	//累计服务费
	public Float getMoney() {
		String sql="select sum(reward) from tb_child_task where result=1 and taskid="+task.getId();
		try{
			return Float.parseFloat(walletDao.findBySql(sql).get(0).toString());
		}catch(Exception e){
			return 0.00F;
		}
	}

	/**
	 * 
		 * @description	检查该任务是否能发布，条件为该酒店钱包余额充足
		 * @author  han1088
	 * @return 
	 */
	public boolean authTask() {
		Hotel hotel = getHotel();
		if(hotel.getState() != Constant.HOTEL_STATE_PASS){
			LogUtil.info("酒店所处的状态不能发布任务酒店ID="+hotel.getId()
					+";状态="+hotel.getState());
			return false;
		}
		List<Wallet> li = walletDao.findByProperty("userid", hotel.getId().toString());
		if(li.size() == 0){//钱包没被创建
			LogUtil.info("钱包没被创建酒店ID="+hotel.getId());
			return false;
		}else{
			float balance = li.get(0).getBalance()+li.get(0).getOverdraft();//获取钱包余额
			if(balance == 0){//余额为0
				LogUtil.info("钱包没钱了;酒店ID="+hotel.getId());
				return false;
				
			}
			int days = DateUtil.daysDistance(task.getStarttime(), task.getEndtime());
			float expectedCost = task.getTaskroomcount()*task.getUnitprice()*days;//预计花费
			if(balance<expectedCost){
				return false;
			}
		}
		return true;
	}

	/**
	 * 
		 * @description	获取最低房量
		 * @author  han1088
		 * @return
	 */
	public float getMinRooms(){
		return getHotel().getTaskroomcount();
	}
	
	/**
	 * 
		 * @description	获取单价
		 * @author  han1088
		 * @return
	 */
	public float getUnitprice(){
		return getHotel().getUnitprice();
	}
	
	/**
	 * 
		 * @description	获取酒店相关信息
		 * @author  han1088
		 * @return
	 */
	public Hotel getHotel() {
		return hotelDao.findById(task.getHotelid());
	}
	
	/**
	 * 
		 * @description	获取当日清洁房量
		 * @author  han1088
		 * @return
	 */
	public int getTodayRoomCount(){
		String sql="select sum(roomcount) from tb_child_task where result=1 "
				+ " and taskid="+task.getId()
				+ " and creattime = '" + DateUtil.formatYMD(new Date());
		try{
			return Integer.parseInt(walletDao.findBySql(sql).get(0).toString());
		}catch(Exception e){
			return 0;
		}
	}
}
